package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.custom.dialog.CustomMyDialog;
import com.ronri.common.WaitDialog;
import com.ronri.iflytek.OnSpeechListening;
import com.ronri.iflytek.VoiceInput;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class DialogActivity extends Activity implements View.OnClickListener {
    private String add_user;
    private String ids;
    private String region_code;
    private String reason;
    private String type;
    private String model;

    private RelativeLayout btnBack;
    private TextView tv_title;
    private TextView tv_right;
    private TextView tv_hit;

    private EditText et_content;

    private RelativeLayout rl_voice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        tv_title = findViewById(R.id.tv_title);
        tv_right = findViewById(R.id.tv_right);
        tv_right.setOnClickListener(this);
        tv_hit = findViewById(R.id.tv_hit);
        et_content = findViewById(R.id.et_content);
        rl_voice = findViewById(R.id.rl_voice);
        rl_voice.setOnClickListener(this);
        add_user = getIntent().getStringExtra("add_user");
        ids = getIntent().getStringExtra("ids");
        region_code = getIntent().getStringExtra("region_code");
        type = getIntent().getStringExtra("type");
        model = getIntent().getStringExtra("model");
        if (model.equals("ent")) {
            if (type.equals("1")) {//通过
                tv_title.setText("审核通过");
                tv_hit.setText("请输入通过描述");
            } else {
                tv_title.setText("审核拒绝");
                tv_hit.setText("请输入拒绝理由");
            }
        } else if (model.equals("patrol")) {
            tv_title.setText("处理结果");
            tv_hit.setText("请输入处理结果");
        } else if (model.equals("supervise")) {
            if (type.equals("1")) {//通过
                tv_title.setText("审核通过");
                tv_hit.setText("请输入通过描述");
            } else {
                tv_title.setText("审核拒绝");
                tv_hit.setText("请输入拒绝理由");
            }
        } else if (model.equals("user")) {
            if (type.equals("1")) {//通过
                tv_title.setText("审核通过");
                tv_hit.setText("请输入通过描述");
            } else {
                tv_title.setText("审核拒绝");
                tv_hit.setText("请输入拒绝理由");
            }
        }else if (model.equals("register")) {
            if (type.equals("3")) {//通过
                tv_title.setText("审核通过");
                tv_hit.setText("请输入通过描述");
            } else {
                tv_title.setText("审核拒绝");
                tv_hit.setText("请输入拒绝理由");
            }
        }else if (model.equals("archive")) {
            if (type.equals("3")) {//通过
                tv_title.setText("审核通过");
                tv_hit.setText("请输入通过描述");
            } else {
                tv_title.setText("审核拒绝");
                tv_hit.setText("请输入拒绝理由");
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
            case R.id.rl_voice:
                VoiceInput.getVoiceText(this, new OnSpeechListening() {
                    @Override
                    public void OnSpeechResult(String szContent) {
                        et_content.setText(szContent);
                        et_content.setSelection(et_content.length());
                    }
                });
                break;

            case R.id.tv_right:
                reason = et_content.getText().toString();
                View v = getWindow().peekDecorView();
                if (v != null) {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                if (model.equals("ent")) {
                    if (reason.isEmpty()) {
                        if (type.equals("1")) {
                            Toast.makeText(DialogActivity.this, "请输入描述文字", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(DialogActivity.this, "请输入拒绝理由", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        CustomMyDialog.Builder builder = new CustomMyDialog.Builder(DialogActivity.this);
                        builder.setMessage("确认提交？");
                        builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                WaitDialog.ShowDialog(DialogActivity.this, "数据加载中...");
                                postCheckEnt(type, ids, reason, add_user, region_code);
                            }
                        });
                        builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.createNormalDialog().show();
                    }
                } else if (model.equals("patrol")) {
                    if (reason.isEmpty()) {
                        Toast.makeText(DialogActivity.this, "请输入处理结果", Toast.LENGTH_SHORT).show();
                    } else {
                        CustomMyDialog.Builder builder = new CustomMyDialog.Builder(DialogActivity.this);
                        builder.setMessage("确认提交？");
                        builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                WaitDialog.ShowDialog(DialogActivity.this, "数据加载中...");
                                postCheckPatrol(ids, reason, add_user, region_code);
                            }
                        });
                        builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.createNormalDialog().show();
                    }
                } else if (model.equals("supervise")) {
                    if (reason.isEmpty()) {
                        if (type.equals("1")) {
                            Toast.makeText(DialogActivity.this, "请输入描述文字", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(DialogActivity.this, "请输入拒绝理由", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        CustomMyDialog.Builder builder = new CustomMyDialog.Builder(DialogActivity.this);
                        builder.setMessage("确认提交？");
                        builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                WaitDialog.ShowDialog(DialogActivity.this, "数据加载中...");
                                postCheckSupervise(reason, add_user, region_code, type, ids);
                            }
                        });
                        builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.createNormalDialog().show();
                    }
                } else if (model.equals("user")) {
                    if (reason.isEmpty()) {
                        if (type.equals("1")) {
                            Toast.makeText(DialogActivity.this, "请输入描述文字", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(DialogActivity.this, "请输入拒绝理由", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        CustomMyDialog.Builder builder = new CustomMyDialog.Builder(DialogActivity.this);
                        builder.setMessage("确认提交？");
                        builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                postCheckUser(type,ids, reason, add_user, region_code);
                            }
                        });
                        builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.createNormalDialog().show();
                    }
                }else if (model.equals("register")) {
                    if (reason.isEmpty()) {
                        if (type.equals("3")) {
                            Toast.makeText(DialogActivity.this, "请输入描述文字", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(DialogActivity.this, "请输入拒绝理由", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        CustomMyDialog.Builder builder = new CustomMyDialog.Builder(DialogActivity.this);
                        builder.setMessage("确认提交？");
                        builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                postCheckRegister(type,ids, reason, add_user, region_code);
                            }
                        });
                        builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.createNormalDialog().show();
                    }
                }else if (model.equals("archive")) {
                    if (reason.isEmpty()) {
                        if (type.equals("3")) {
                            Toast.makeText(DialogActivity.this, "请输入描述文字", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(DialogActivity.this, "请输入拒绝理由", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        CustomMyDialog.Builder builder = new CustomMyDialog.Builder(DialogActivity.this);
                        builder.setMessage("确认提交？");
                        builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                postCheckArchive(type,ids, reason, add_user, region_code);
                            }
                        });
                        builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.createNormalDialog().show();
                    }
                }
                break;
                default:
                    break;
        }
    }
    void postCheckArchive(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.markUrl + "ent/checkEnt.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(DialogActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    void postCheckRegister(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.markUrl + "register/checkRegister.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(DialogActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    void postCheckUser(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.markUrl + "register/checkUser.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(DialogActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    void postCheckSupervise(String reason, String add_user, String region_code, String type, String json) {
        OkHttpUtils
                .post()
                .url(InitApplication.newUrl + "supervise/checkSuperviseList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .addParams("type", type)
                .addParams("json", json)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                        WaitDialog.HideDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        WaitDialog.HideDialog();
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(DialogActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

//    void postCheckSupervise(String ids, String reason, String add_user, String region_code, String type, String status) {
//        OkHttpUtils
//                .post()
//                .url(InitApplication.newUrl + "supervise/checkSupervise.json")
//                .addParams("addFrom", "1")
//                .addParams("addMark", "android")
//                .addParams("ids", ids)
//                .addParams("reason", reason)
//                .addParams("add_user", add_user)
//                .addParams("region_code", region_code)
//                .addParams("type", type)
//                .addParams("status", status)
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//                        Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        try {
//                            JSONObject jsonObject = new JSONObject(response);
//                            int code = jsonObject.optInt("code");
//                            if (code == 0) {
//                                Toast.makeText(DialogActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
//                                setResult(Activity.RESULT_OK);
//                                finish();
//                            } else {
//                                Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
//                            }
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
//    }

    void postCheckPatrol(String ids, String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.newUrl + "check/operateCheck.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("ids", ids)
                .addParams("result", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                        WaitDialog.HideDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        WaitDialog.HideDialog();
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(DialogActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    void postCheckEnt(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.newUrl + "ent/checkEnt.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                        WaitDialog.HideDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        WaitDialog.HideDialog();
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(DialogActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(DialogActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
